package cn.angis.common.config;

import io.lettuce.core.api.StatefulRedisConnection;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * 包名称：cn.angis.common.config
 * 类名称：RedisUtils
 * 类描述：
 * 创建人：@author angis.cn
 * 创建日期： 2024/2/23 17:17
 */
@Component
public class RedisUtils {
    @Inject
    private StatefulRedisConnection<String, Object> statefulRedisConnection;
    /**
     * 写入缓存设置时效时间
     *
     * @param key
     * @param value
     * @param expireTime 有效时间，单位秒
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            statefulRedisConnection.sync().set(key, value);
            statefulRedisConnection.sync().expire(key, expireTime);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Object get(String key) {
        return statefulRedisConnection.sync().get(key);
    }
}
